package com.yc.navy.dayfresh.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.navy.dayfresh.entity.Fav;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author navy
 * @company 源辰信息
 * @date 2024/9/9
 * @email haijunzhou@hnit.edu.cn
 */
public interface FavMapper extends BaseMapper<Fav> {

    @Select("select * from fav a where" +
            " uid = #{uid} and ifnull(tags,'') = ''")
    List<Fav> selectByNoTags(int uid);

    @Select("SELECT\n" +
            "	min( ifnull( sort, a.id )) - 1 \n" +
            "FROM\n" +
            "	fav a\n" +
            "	JOIN tag_fav b ON a.id = b.fid \n" +
            "WHERE\n" +
            "	b.tid IN (\n" +
            "	SELECT\n" +
            "		id \n" +
            "	FROM\n" +
            "		tag a \n" +
            "WHERE\n" +
            "	EXISTS ( SELECT * FROM tag_fav b WHERE a.id = b.tid AND b.fid = #{fid} ))")
    int selectMinSort(int fid);

    @Select("SELECT a.* FROM fav a INNER JOIN tag_fav b ON a.id = b.fid WHERE b.tid = #{tid}")
    List<Fav> selectFavByTid(Integer tid);

    @Select("select * from fav where uid=#{uid} and name like concat('%',#{name},'%')")
    List<Fav> toFind(@Param("uid") int uid, @Param("name") String name);
}
